//
//  MCOrderListViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/20.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

protocol MCAfterSaleListViewControllerDelegate : NSObjectProtocol {
    func MCAfterSaleListViewControllerPushTo(viewController:UIViewController)
}

class MCAfterSaleListViewController: MCViewController {

    weak var delegate : MCAfterSaleListViewControllerDelegate?
    var pageNumber = 1
    var status = 0
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        pageNumber = 1
        sendNetworking()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "status"  : status,
            "pageNum" : pageNumber
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_getStoreAfterSales, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            
            let array = response["data"] as? NSArray ?? []
            
            if array.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                
                if array.count == 0 {
                    self.tableView.mj_footer.endRefreshingWithNoMoreData()
                }
                
                for dict in array {
                    let fatherModel = MCAfterSaleListModel()
                    fatherModel.setValuesForKeys(dict as! [String : Any])
                    self.dataArrayM.add(fatherModel)
                }
                self.tableView.reloadData()
            }
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.view.backgroundColor = UIColor.white
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, CGFloat(MCSafeAreaBottomHeight), 0))
        }
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
}

extension MCAfterSaleListViewController : UITableViewDelegate,UITableViewDataSource {
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 185
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let model = self.dataArrayM[indexPath.row] as! MCAfterSaleListModel
        
        let cell = MCAfterSaleListCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.model = model
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = self.dataArrayM[indexPath.row] as! MCAfterSaleListModel
        
        let vc = MCAfterSaleDetailViewController()
        vc.model = model
        delegate?.MCAfterSaleListViewControllerPushTo(viewController: vc)
    }
}

